package com.gtpickupapp;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class SearchEventsGroup extends ActivityGroup {
	public AlertDialog.Builder exitAlert;
	public static SearchEventsGroup group;
	private ArrayList<View> history;
	
	
	@Override  
    protected void onCreate(Bundle savedInstanceState) {  
          super.onCreate(savedInstanceState);  
          this.history = new ArrayList<View>();
          setupAlerts();
          group = this;  
          Bundle bundle = getIntent().getExtras();
          Intent intent = new  Intent(this,SearchEvents.class);
          intent.putExtras(bundle);
              // Start the root activity within the group and get its view  
          View view = getLocalActivityManager().startActivity("search",  intent 
                                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
                                            .getDecorView();  
  
              // Replace the view of this ActivityGroup  
          replaceView(view);  
  
       }  
	
	 public void replaceView(View v) {  
         // Adds the old one to history  
		 history.add(v);  
         // Changes this Groups View to the new View.  
		 setContentView(v);  
	 }
	 
	 public void back() {  
		 //TODO WTF??? 000000
         if(history.size() > 1) {  
        	 
             history.remove(history.size()-1);  
             setContentView(history.get(history.size()-1));  
           
         }else { 
        	 exitAlert.create().show();
             //finish();  
         }  
	  } 
	 
	 @Override  
	 public void onBackPressed() {  
		 SearchEventsGroup.group.back();  
	     return;  
	 }
	 
	 public void setupAlerts(){
		 exitAlert = new AlertDialog.Builder(this);
	        exitAlert.setMessage("Are You Sure You Want To Exit The Application")
	        	.setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				})
				.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
	        
	 }
}
